package com.cxh.rmsq.ui.fragment.self;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.cxh.rmsq.R;
import com.cxh.rmsq.ui.activity.self.PublishCommunityActivity;
import com.cxh.rmsq.ui.fragment.BaseFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

/**
 * 社交圈
 */
public class SocialCircleFragment extends BaseFragment {

    private final String[] tabs = new String[]{/*"动态", "圈子", "订阅",*/ "好友"};
    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    private TabLayoutMediator mediator;

    private ViewPager2 viewPager;
    private TabLayout tabCommunity;
    private ImageView ivPublish;

    public SocialCircleFragment() {
        // Required empty public constructor
    }

    public static SocialCircleFragment newInstance() {
        SocialCircleFragment fragment = new SocialCircleFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_social_circle;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = view.findViewById(R.id.view_pager );
        tabCommunity = view.findViewById(R.id.tab_community);
        ivPublish = view.findViewById(R.id.iv_publish);
        mFragments.clear();

//        mFragments.add(CommunityTrendsFragment.newInstance("1"));
//        mFragments.add(new CircleFragment());
//        mFragments.add(CommunityTrendsFragment.newInstance("2"));
        mFragments.add(new FriendCommunityFragment());

//        Fragment activityFragment = new WebFragment();
//        Bundle valueBundle = new Bundle();
//        valueBundle.putString("webLoadModel", "homePage");
//        valueBundle.putString("webUrl", AppWebUrl.COMMUNITY_ACTIVITY);
//        activityFragment.setArguments(valueBundle);
//        mFragments.add(activityFragment);

        //Adapter
        viewPager.setAdapter(new FragmentStateAdapter(getChildFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                //FragmentStateAdapter内部自己会管理已实例化的fragment对象。
                // 所以不需要考虑复用的问题
                return mFragments.get(position);
            }

            @Override
            public int getItemCount() {
                return mFragments.size();
            }
        });
        //viewPager 页面切换监听监听
        viewPager.registerOnPageChangeCallback(changeCallback);
//        binding.viewPager.setUserInputEnabled(false);
        viewPager.setOffscreenPageLimit(mFragments.size() /*- 1*/);

        mediator = new TabLayoutMediator(tabCommunity, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                //这里可以自定义TabView
                View customView = tab.getCustomView();
                if (customView == null) {
                    tab.setCustomView(R.layout.tab_item_camp);
                }
                TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                ImageView tab_indicator = tab.getCustomView().findViewById(R.id.tab_indicator);
                if (position < tabs.length) {
                    tab_text.setText(tabs[position]);
                }
                tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
                tab_text.setTypeface(Typeface.DEFAULT_BOLD);
                tab_text.setTextColor(Color.parseColor("#e0000000"));
                tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
            }
        });
        //要执行这一句才是真正将两者绑定起来
        mediator.attach();

        ivPublish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //发布
                getActivity().startActivity(new Intent(getActivity(), PublishCommunityActivity.class)
                        .putExtra("id",""));
            }
        });
    }

    @Override
    protected void onInitView(Bundle savedInstanceState, Intent intent) {

    }

    private final ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            //可以来设置选中时tab的大小
            int tabCount = tabCommunity.getTabCount();
            for (int i = 0; i < tabCount; i++) {
                TabLayout.Tab tab = tabCommunity.getTabAt(i);
                if (tab != null) {
                    View customView = tab.getCustomView();
                    if (customView != null) {
                        TextView tab_text = tab.getCustomView().findViewById(R.id.tab_text);
                        ImageView tab_indicator = tab.getCustomView().findViewById(R.id.tab_indicator);
//                        if (position == 0) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        } else if (position == 1) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        } else if (position == 2) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        }else if (position == 3) {
//                            tab_text.setTextColor(Color.parseColor("#e0000000"));
//                            tab_indicator.setImageResource(R.mipmap.icon_tab_indicator);
//                        }
                        if (tab.getPosition() == position) {
                            tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
                            tab_text.setTypeface(Typeface.DEFAULT_BOLD);
                            tab_indicator.setVisibility(View.VISIBLE);
                            tab_text.setTextColor(Color.parseColor("#e0000000"));
                        } else {
                            tab_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
                            tab_text.setTypeface(Typeface.DEFAULT);
                            tab_indicator.setVisibility(View.INVISIBLE);
                            tab_text.setTextColor(Color.parseColor("#666666"));
                        }
                    }
                }
            }
        }
    };
}